package cn.ibizlab.odoo.core.odoo_utm.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iutm_mixin;
import cn.ibizlab.odoo.core.odoo_utm.domain.Utm_mixin;
import cn.ibizlab.odoo.core.odoo_utm.filter.Utm_mixinSearchContext;
import cn.ibizlab.odoo.core.odoo_utm.service.IUtm_mixinService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[UTM Mixin] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Utm_mixinServiceImpl implements IUtm_mixinService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iutm_mixinClientService utm_mixinClientService;

    @Override
    public boolean update(Utm_mixin utm_mixin) {
        boolean bOk = false ;
        Iutm_mixin clientModel = this.convert2Model(utm_mixin,null);
        utm_mixinClientService.update(clientModel) ;
        this.convert2Domain(clientModel,utm_mixin) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Utm_mixin get(Integer id) {
        Iutm_mixin clientModel = utm_mixinClientService.createModel();
        clientModel.setId(id);
        utm_mixinClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Utm_mixin get(Utm_mixin utm_mixin) {
        Utm_mixin retutm_mixin = this.get(utm_mixin.getId()) ;
    	BeanCopier copier=BeanCopier.create(retutm_mixin.getClass(), utm_mixin.getClass(), false);
        copier.copy(retutm_mixin, utm_mixin, null);
        return utm_mixin ;
    }

    @Override
    public boolean create(Utm_mixin utm_mixin) {
        boolean bOk = false ;
        Iutm_mixin clientModel = this.convert2Model(utm_mixin,null);
        utm_mixinClientService.create(clientModel) ;
        this.convert2Domain(clientModel,utm_mixin);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //utm_mixinClientService.removeBatch(utm_mixins) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iutm_mixin clientModel = utm_mixinClientService.createModel();
        clientModel.setId(id);
        utm_mixinClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Utm_mixin> utm_mixins, int batchSize) {

        //utm_mixinClientService.updateBatch(utm_mixins,batchSize) ;

    }

    @Override
    public void createBatch(List<Utm_mixin> utm_mixins, int batchSize) {

                        //utm_mixinClientService.createBatch(utm_mixins,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Utm_mixin> searchDefault(Utm_mixinSearchContext context) {
        List<Utm_mixin> list = new ArrayList<Utm_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iutm_mixin> clientModelList = utm_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iutm_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Utm_mixin>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据UTM 营销活动查询
     */
    @Override
    public Page<Utm_mixin> selectByOdoo_campaign(Integer id){
        Utm_mixinSearchContext context = new Utm_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Utm_mixin> list = new ArrayList<Utm_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iutm_mixin> clientModelList = utm_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iutm_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Utm_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM媒体查询
     */
    @Override
    public Page<Utm_mixin> selectByOdoo_medium(Integer id){
        Utm_mixinSearchContext context = new Utm_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Utm_mixin> list = new ArrayList<Utm_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iutm_mixin> clientModelList = utm_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iutm_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Utm_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM来源查询
     */
    @Override
    public Page<Utm_mixin> selectByOdoo_source(Integer id){
        Utm_mixinSearchContext context = new Utm_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Utm_mixin> list = new ArrayList<Utm_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iutm_mixin> clientModelList = utm_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iutm_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Utm_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iutm_mixin convert2Model(Utm_mixin domain , Iutm_mixin model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = utm_mixinClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getMedium_id_textDirtyFlag())
            model.setMedium_id_text(domain.getMedium_id_text());
         if(domain.getSource_id_textDirtyFlag())
            model.setSource_id_text(domain.getSource_id_text());
         if(domain.getCampaign_id_textDirtyFlag())
            model.setCampaign_id_text(domain.getCampaign_id_text());
         if(domain.getMedium_idDirtyFlag())
            model.setMedium_id(domain.getMedium_id());
         if(domain.getSource_idDirtyFlag())
            model.setSource_id(domain.getSource_id());
         if(domain.getCampaign_idDirtyFlag())
            model.setCampaign_id(domain.getCampaign_id());
        return model ;
    }

    public Utm_mixin convert2Domain( Iutm_mixin model ,Utm_mixin domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Utm_mixin();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getMedium_id_textDirtyFlag())
            domain.setMedium_id_text(model.getMedium_id_text());
        if(model.getSource_id_textDirtyFlag())
            domain.setSource_id_text(model.getSource_id_text());
        if(model.getCampaign_id_textDirtyFlag())
            domain.setCampaign_id_text(model.getCampaign_id_text());
        if(model.getMedium_idDirtyFlag())
            domain.setMedium_id(model.getMedium_id());
        if(model.getSource_idDirtyFlag())
            domain.setSource_id(model.getSource_id());
        if(model.getCampaign_idDirtyFlag())
            domain.setCampaign_id(model.getCampaign_id());
        return domain ;
    }


}
